package com.abdulaziz.ms.view;

import org.eclipse.gef.EditPart;
import org.eclipse.jface.viewers.ArrayContentProvider;
import org.eclipse.jface.viewers.ColumnLabelProvider;
import org.eclipse.jface.viewers.ISelection;
import org.eclipse.jface.viewers.StructuredSelection;
import org.eclipse.jface.viewers.TableViewer;
import org.eclipse.jface.viewers.TableViewerColumn;
import org.eclipse.jface.viewers.TreeViewer;
import org.eclipse.swt.SWT;
import org.eclipse.swt.awt.SWT_AWT;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.events.SelectionListener;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Group;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.TabFolder;
import org.eclipse.swt.widgets.TabItem;
import org.eclipse.swt.widgets.Table;
import org.eclipse.swt.widgets.Text;
import org.eclipse.ui.IEditorReference;
import org.eclipse.ui.ISelectionListener;
import org.eclipse.ui.IWorkbenchPage;
import org.eclipse.ui.IWorkbenchPart;
import org.eclipse.ui.IWorkbenchPartSite;
import org.eclipse.ui.PlatformUI;
import org.eclipse.ui.internal.dialogs.ViewContentProvider;
import org.eclipse.ui.internal.dialogs.ViewLabelProvider;
import org.eclipse.ui.part.IPage;
import org.eclipse.ui.part.MessagePage;
import org.eclipse.ui.part.PageBook;
import org.eclipse.ui.part.PageBookView;
import org.eclipse.ui.part.ViewPart;
import org.eclipse.ui.part.WorkbenchPart;

import com.abdulaziz.ms.OCV.UMLClass;
import com.abdulaziz.ms.OCV.UMLVariable;
import com.abdulaziz.ms.ocv.editor.OCVMutiPageEditor;
import com.abdulaziz.ms.ocv.gef.editor.part.OCVClassEditPart;

public class CustomizedViewPart extends ViewPart implements ISelectionListener{

	private Label lable;
	private Button updateButton;
	private UMLClass umlClass;
	private OCVMutiPageEditor editor;
	
	@Override
	public void createPartControl(Composite parent) {

		TabFolder tabFolder = new TabFolder(parent, SWT.BORDER);
		
		TabItem classTab = new TabItem(tabFolder, SWT.NULL);
		classTab.setText("Class Tab ");
		
		TabItem attributeTab = new TabItem(tabFolder, SWT.NULL);
		attributeTab.setText("Attributes ");

		TabItem methodTab = new TabItem(tabFolder, SWT.NULL);
		methodTab.setText("Methods ");
		
		
		TabItem operationContractTab = new TabItem(tabFolder, SWT.NULL);
		operationContractTab.setText("Operation Contracts ");


		
		Group classTabGroup = new Group(tabFolder, SWT.NONE);
		
		lable = new Label(classTabGroup, SWT.NONE);
		lable.setText("Hello"); /// ---- at the start of the application 
		lable.setBounds(10, 10, 10, 100);
		updateButton = new Button(classTabGroup, SWT.CENTER);
		updateButton.setText("Add new VContract");
		updateButton.setBounds(100, 50, 130, 25);
		
		Button updateButton2 = new Button(classTabGroup, SWT.CENTER);
		updateButton2.setText("Delete VContract");
		updateButton2.setBounds(400, 50, 100, 30);
	
		classTab.setControl(classTabGroup);
	
		
	//  1.1 register for Selection Listener -> selectionChanged(IWorkbenchPart part, ISelection selection) 

		getViewSite().getPage().addSelectionListener(this);

		// End 1.1

		// -- 1.2 get all editors
		IEditorReference[] viewReferences = PlatformUI.getWorkbench().getActiveWorkbenchWindow().getActivePage().getEditorReferences();
		
		for (int i = 0; i < viewReferences.length; i++) {	
			if (viewReferences[i].getEditor(false) instanceof OCVMutiPageEditor) {
				editor = ((OCVMutiPageEditor) viewReferences[i].getEditor(false));
			}
		}

		// --End 1.2
		

		updateButton.addSelectionListener(new SelectionListener() {
			
			@Override
			public void widgetSelected(SelectionEvent e) {
				
				if(editor != null)
				{
					editor.createVContractPage();

				}
				if(umlClass !=null)
				{
					
					lable.setText("Done" );
					umlClass.setConstraints(umlClass.getConstraints().expand(12, 12));
					
					
					
				}
				
				
				
			}
			
			@Override
			public void widgetDefaultSelected(SelectionEvent e) {
				// TODO Auto-generated method stub
				
			}
		});
		
updateButton2.addSelectionListener(new SelectionListener() {
			
			@Override
			public void widgetSelected(SelectionEvent e) {
				
				if(editor != null)
				{
					editor.removePage(editor.getActivePage());

				}
				if(umlClass !=null)
				{
					
					lable.setText("Remove is done ... " );
				
					
					
				}
				
				
				
			}
			
			@Override
			public void widgetDefaultSelected(SelectionEvent e) {
				// TODO Auto-generated method stub
				
			}
		});
		
	}

	@Override
	public void setFocus() {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void selectionChanged(IWorkbenchPart part, ISelection selection) {
		umlClass = null;
		lable.setText("");
		StructuredSelection structredSelection = (StructuredSelection) selection;
		Object firstElement = structredSelection.getFirstElement();
		
	
		if(firstElement instanceof OCVClassEditPart)
		{  // works when a class has been selected 
			UMLClass classA = (UMLClass) ((OCVClassEditPart) firstElement).getModel();

			String m ="";
			for (UMLVariable umlVariable : classA.getClassAttributes()) {
				m= m+":::"+ umlVariable.getVariableName() + "," +umlVariable.getVariableType() ;
				
				
			}
			lable.setText(classA.getClassName() +"    " +m);
			
			umlClass = classA;
			
			
		

				
			
		}
		
		
		
	//	Object a = selection.
		
		
		/*
		if (part instanceof EditPart) {
			Object model = ((EditPart) part).getModel();
			if(model instanceof UMLClass)
			{
				UMLClass model2 = ((UMLClass) model);
				lable.setText(part.getTitle() + " " +model2.getClassName());

			}else
				lable.setText("Edit ------ "+part.getTitle());

			}
		else
			lable.setText(part.getTitle() );
*/
		}
}